#include <vector>
#include "CAlliance.h"
#include "CParty.h"
#include "CChar.h"
#include "CDefinePartyPacket.h"
#include "define.h"

CDefinePartyPacket::CDefinePartyPacket(CParty * party) {
	int nextPosition = 1;
	unsigned int partyFlags = 0;
	
	memset(this->data, 0, 0x54*2);
	this->type = 0xC8;
	this->size = 0x54;

	//Verify they have a party; otherwise it will put them back into solo mode
	if ( party != NULL) {
		//Check to see if the party is part of an alliance
		if ( (party->inAlliance > 0) && (party->getAlliance() != NULL) ) {
			party->getAlliance()->lockAllianceList();
			//The party is part of an alliance, now check all of the parties in the alliance for members
			for(unsigned int x = 0; x < party->getAlliance()->partyList.size(); x++) {
				//Get the first party from the alliance
				CParty * subParty = party->getAlliance()->partyList.at(x);
				//Verify the pointer of the party is not null.
				if (subParty != NULL) {
					//Check every member inside of the alliance party
					subParty->lockPartyList();
					for (unsigned int y = 0; y < subParty->members.size(); y++) {
						CChar * partyChar = subParty->members.at(y);
						if (partyChar != NULL) {
							//set the party flags to represent our party
							partyFlags = x;

							//Check to see if the alliance member is a party leader
							if (subParty->getLeader() == partyChar)
								partyFlags += PARTY_LEADER;

							//Check to see if the alliance member is the alliance leader
							if (party->getAlliance()->getMainParty()->getLeader() == partyChar)
								partyFlags += ALLIANCE_LEADER;

							//Check to see if the alliance member is a quarter master
							if (party->getAlliance()->getMainParty()->getQM() == partyChar)
								partyFlags += PARTY_QM;

							//Copy the player information to the alliance packet

							//charid of the player
							memcpy(this->data+((0x08 * nextPosition)-4),&partyChar->m_id, sizeof(unsigned int));
							
							//targID of the player
							unsigned short targId = partyChar->getTargId();
							memcpy(this->data+((0x08 * nextPosition)),&targId, sizeof(unsigned short));

							//the party flags calculated above
							memcpy(this->data+((0x08 * nextPosition)+2),&partyFlags, sizeof(char));
							
							//Zone of the player
							//Get prevzone if player is in Moghouse
							this->data[((0x08 * nextPosition)+3)] = partyChar->getDisplayZone();
							nextPosition++;
						}
					}
					subParty->unlockPartyList();
				}
			}
			party->getAlliance()->unlockAllianceList();

			//Not in an alliance
		} else {
			//Check every member inside of the party
			party->lockPartyList();
			for (unsigned int y = 0; y < party->members.size(); y++) {
				CChar * partyChar = party->members.at(y);
				if (partyChar != NULL) {
					//set the party flags to 0 so we have a clean slate
					partyFlags = 0;
					
					//Check to see if the alliance member is a party leader
					if (party->getLeader() == partyChar)
						partyFlags += PARTY_LEADER;

					//Check to see if the alliance member is a quarter master
					if (party->getQM() == partyChar)
						partyFlags += PARTY_QM;

					//Copy the player information to the alliance packet

					//charid of the player
					memcpy(this->data+((0x08 * nextPosition)-4),&partyChar->m_id, sizeof(unsigned int));
					
					//targID of the player
					unsigned short targId = partyChar->getTargId();
					memcpy(this->data+((0x08 * nextPosition)),&targId, sizeof(unsigned short));

					//the party flags calculated above
					memcpy(this->data+((0x08 * nextPosition)+2),&partyFlags, sizeof(char));
					
					//Zone of the player
					//Get prevzone if player is in Moghouse
					this->data[((0x08 * nextPosition)+3)] = partyChar->getDisplayZone();
					nextPosition++;
				}
			}
			party->unlockPartyList();
		}
	}
}

CDefinePartyPacket::~CDefinePartyPacket() {

}
